package com.mall.controller;


import cn.hutool.core.bean.BeanUtil;
import com.mall.constant.StatusConstant;
import com.mall.dto.GoodsDTO;
import com.mall.dto.GoodsPageQueryDTO;
import com.mall.po.Goods;
import com.mall.result.PageResult;
import com.mall.result.Result;
import com.mall.service.GoodsService;
import com.mall.vo.GoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author itlxc
 * @since 2024-04-18
 */
@RestController
@RequestMapping("/admin/goods")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "商品管理相关接口")
public class GoodsController {

    private final GoodsService goodsService;

    /**
     * 添加商品
     * @param goodsDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加商品")
    public Result saveGoods(@RequestBody GoodsDTO goodsDTO) {
        log.info("添加商品：{}", goodsDTO);
        Goods goods = BeanUtil.copyProperties(goodsDTO, Goods.class);
        goods.setStatus(StatusConstant.DISABLE);
        goodsService.save(goods);
        return Result.success();
    }

    /**
     * 商品分页查询
     * @param goodsPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("商品分页查询")
    public Result<PageResult> goodsPageQuery(GoodsPageQueryDTO goodsPageQueryDTO) {
        log.info("商品分页查询：{}", goodsPageQueryDTO);
        PageResult pageResult = goodsService.goodsPageQuery(goodsPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商品")
    public Result<GoodsVO> getGoodsById(@PathVariable Long id) {
        log.info("根据id查询商品：{}", id);
        Goods goods = goodsService.getById(id);
        GoodsVO goodsVO = BeanUtil.copyProperties(goods, GoodsVO.class);
        return Result.success(goodsVO);
    }

    /**
     * 编辑商品信息
     * @param goodsDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("编辑商品信息")
    public Result updateGoods(@RequestBody GoodsDTO goodsDTO) {
        log.info("编辑商品信息：{}", goodsDTO);
        Goods goods = BeanUtil.copyProperties(goodsDTO, Goods.class);
        goodsService.updateById(goods);
        return Result.success();
    }

    /**
     * 删除商品
     * @param ids
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation("删除商品")
    public Result deleteGoods(@RequestParam List<Long> ids) {
        log.info("删除商品：{}", ids);
        goodsService.deleteGoods(ids);
        return Result.success();
    }

    /**
     * 启售、禁售商品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启售、禁售商品")
    public Result startOrStopGood(@PathVariable Integer status, Long id) {
        log.info("启用、禁用商品: id: {}, status: {}", id, status == 1 ? "启用" : "禁用");
        goodsService.startOrStopGood(status, id);
        return Result.success();
    }

}
